package com.cskaoyan.mall.product.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cskaoyan.mall.common.result.Result;
import com.cskaoyan.mall.product.dto.SkuInfoDTO;
import com.cskaoyan.mall.product.dto.SkuInfoPageDTO;
import com.cskaoyan.mall.product.query.SkuInfoParam;
import com.cskaoyan.mall.product.service.SkuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/product")
public class AdminSkuController {
    @Autowired
    SkuService skuService;

    @PostMapping("/saveSkuInfo")
    public Result saveSkuInfo(@RequestBody SkuInfoParam skuInfo){
        skuService.saveSkuInfo(skuInfo);
        return Result.ok(null);
    }

    @GetMapping("/list/{page}/{limit}")
    public Result list(@PathVariable int limit, @PathVariable int page){
        SkuInfoPageDTO skuServicePage = skuService.getPage(new Page<>(page, limit));
        return Result.ok(skuServicePage);
    }

    @GetMapping("/onSale/{skuId}")
    public Result onSale(@PathVariable Long skuId){
        //商品上架
        skuService.onSale(skuId);

        return Result.ok();
    }

    @GetMapping("/cancelSale/{skuId}")
    public Result cancelSale(@PathVariable Long skuId){
        skuService.offSale(skuId);

        return Result.ok();
    }

    @GetMapping("/skuInfo/{skuId}")
    public Result skuInfo(@PathVariable Long skuId){
        SkuInfoDTO skuInfoCache = skuService.getSkuInfo(skuId);
        return Result.ok(skuInfoCache);
    }

}
